<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Activitystream_Actor implements Atom_ChildElement {
    
    /**
     * @var Array of Atom_ChildElement
     */
    private $childElements = array();
    
    const ELEMENT_ACTOR = "actor";
    
    //public function addChildElement(Atom_ChildElement $childElement) {
    // This will soon be old... Doing hacks.
    public function addChildElement($childElement) {
        $this->childElements[] = $childElement;
    }
    
    /**
     * This method should produce something like this:
     * 
     * <activity:actor>
     *  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
     *  <id>http://identi.ca/user/52467</id>
     *  <title>Tuomas Koski</title>
     *  <link rel="alternate" type="text/html" href="http://identi.ca/tkoski"/>
     * </activity:actor>
     * 
     * @param DOMDocument$dom
     */
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(ActivityStream::AS_NS_PREFIX . ':' . self::ELEMENT_ACTOR);
        foreach ($this->childElements as $childElement) {
            $element->appendChild($childElement->asDomElement($dom));
        }
        return $element;
    }
    
    /**
     * <activity:actor>
     *  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
     *  <id>http://www.lobstermonster.org/profile/koski</id>
     *  <title>Tuomas Koski</title>
     *  <link rel="alternate" type="text/html" href="http://www.lobstermonster.org/profile/koski"/>
     *  <link rel="avatar" type="image/png" href="http://www.gravatar.com/avatar/a0c6e63b9b8b8d4f6faa3c9ea62f43bf.png"/>
     * </activity:actor>
     * 
     * @param DOMElement $elm
     * @return Activitystream_Actor
     */
    public static function buildFromDOMElement(DOMElement $elm) {
        $actor = new Activitystream_Actor();
        
        $actor->addChildElement(Activitystream_ObjectType::buildFromDOMElement($elm->getElementsByTagName(Activitystream_ObjectType::ELEMENT_OBJECTTYPE)->item(0)));
        
        $actor->addChildElement(Atom_Entry_Id::buildFromDOMElement($elm->getElementsByTagName(Atom_Entry_Id::ELEMENT_ID)->item(0)));
        
        $title = $elm->getElementsByTagName(Atom_Entry_Title::ELEMENT_TITLE)->item(0);
        if($title != null) {
            //title of actor is empty for cliqset
            $actor->addChildElement(Atom_Entry_Title::buildFromDOMElement($title));
        }
        
        foreach ($elm->getElementsByTagName(Atom_Entry_Link::ELEMENT_LINK) as $elmLink) {
            
            if( $elmLink->parentNode->nodeName !== 'activity:actor') {
                continue;
            }
            
            if($elmLink->getAttribute(Atom_Entry_Link_AlternateHtml::ATTRIBUTE_REL) == Atom_Entry_Link_AlternateHtml::REL_VALUE) {
                $link = Atom_Entry_Link_AlternateHtml::buildFromDOMElement($elmLink); 
                $actor->addChildElement($link);
            } else if($elmLink->getAttribute(Atom_Entry_Link_Avatar::ATTRIBUTE_REL) == Atom_Entry_Link_Avatar::REL_VALUE
                      && $elmLink->getAttribute(Atom_Entry_Link_Avatar::ATTRIBUTE_TYPE) == Atom_Entry_Link_Avatar::TYPE_VALUE ) {
                $link = Atom_Entry_Link_Avatar::buildFromDOMElement($elmLink); 
                $actor->addChildElement($link);
            } else if($elmLink->getAttribute(Atom_Entry_Link_Photo::ATTRIBUTE_REL) == Atom_Entry_Link_Photo::REL_VALUE ) {
                $link = Atom_Entry_Link_Photo::buildFromDOMElement($elmLink); 
                $actor->addChildElement($link);
            } 
            
            //$actor->addChildElement(Atom_Feed_Link::buildFromDOMElement($elmLink));
        }
        
        return $actor;
    }
}